package com.study;

import java.util.Iterator;
import java.util.function.Consumer;

public class SinglyLinkedList implements Iterable<Integer>
{
    private Node head;

    @Override
    public Iterator<Integer> iterator()
    {
        return new Iterator<Integer>()
        {
            Node p = head;

            @Override
            public boolean hasNext()
            {
                return p != null;
            }

            @Override
            public Integer next()
            {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    /**
     * 节点类
     */
    private static class Node
    {
        int value; // 值
        Node next; // 下一个节点指针

        public Node(int value, Node next)
        {
            this.value = value;
            this.next = next;
        }
    }

    // for 遍历
    public void loop1()
    {
        for (Node p = head; p != null; p = p.next) {
            // 做一些事
        }
    }

    // while 遍历
    public void loop()
    {
        Node p = head;
        while (p != null) {
            // 做一些事
            p = p.next;
        }
    }


    public void loop3(Consumer<Integer> before,
                      Consumer<Integer> after)
    {
        recursion(head, before, after);
    }

    private void recursion(Node curr,
                           Consumer<Integer> before, Consumer<Integer> after)
    {
        // 某个节点要进行的操作
        if (curr == null) {
            return;
        }

        before.accept(curr.value);
        recursion(curr.next, before, after);
        after.accept(curr.value);
    }

    public void addFirst(int value)
    {
        head = new Node(value, head);
    }

    public Node findLast()
    {
        if (head == null) return null;
        Node last = head;
        while (last.next != null) {
            last = last.next;
        }
        return last;
    }

    public void addLast(int value)
    {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, null);
    }

    private Node findNode(int index)
    {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index)
                return p;
        }
        return null;
    }

    /**
     * 根据索引查找
     *
     * @param index 索引
     * @return 找到, 返回该索引位置节点的值
     * @throws IllegalArgumentException 找不到, 抛出 index 非法异常
     */
    public int get(int index) throws IllegalArgumentException
    {
        Node node = findNode(index);
        if (node == null) {
            throw illegalIndex(index);
        }
        return node.value;
    }

    private IllegalArgumentException illegalIndex(int index)
    {
        return new IllegalArgumentException(
                String.format("index [%d] 不合法%n", index));
    }

    public void insert(int index, int value) throws IllegalArgumentException
    {
        if (index == 0) {
            addFirst(value);
            return;
        }
        Node prev = findNode(index - 1);
        if (prev == null) throw illegalIndex(index);

        prev.next = new Node(value, prev.next);
    }

    /**
     * 删除第一个
     *
     * @throws IllegalArgumentException - 如果不存在, 抛出 index 非法异常
     */
    public void removeFirst() throws IllegalArgumentException
    {
        if (head == null) {
            throw illegalIndex(0);
        }
        head = head.next;
    }

    public void remove(int index) throws IllegalArgumentException
    {
        if (index == 0) {
            removeFirst();
            return;
        }
        Node prev = findNode(index - 1);
        if (prev == null) throw illegalIndex(index);

        Node removed = prev.next;
        if (removed == null) throw illegalIndex(index);

        prev.next = removed.next;
    }
}
